<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @copyright (c) 2013
 * @author Babak Taraghi 'babak.taraghi@gmail.com'
 * @license Apache License 2.0
 * @version 1.0
 * @desc Logical user related actions
 */
class User extends CI_Controller {



	/**
	 * Defining controller properties
	 */
	public $viewData = array();
	private $language = "english";
	private $action;


	/**
	 *
	 * loading common required models and langs
	 */
	public function __construct(){
		parent::__construct();

		$this->load->helper("common_action");
		$this->action = new Action($this);

		$this->lang->load('common', $this->language);
		$this->lang->load('user', $this->language);

		$this->load->model("simple_data_model","simpleDataModel");
		$this->load->model("tag_model","tagModel");

		$this->simpleDataModel->init("USER","UID");
		$this->tagModel->init();
	}


	function test(){
		$this->load->view("testUI");
	}


	/**
	 *
	 * User register action
	 */
	public function register(){

		$this->action->addJSONItem("tag", $this->tagModel->getTag("register"));
		$this->action->addJSONItem("success", 0);

		$name = strtolower($this->input->post("username"));
		$email = strtolower($this->input->post("email"));
		$password = $this->input->post("password");
		$confirmPassword = $this->input->post("confirm_password");

		// check to see if the required inputs are valid
		if($name && $email && $password && $confirmPassword){

			if($password === $confirmPassword){
					
				// search for existance of name and email, no duplicate is allowed
				$records = $this->simpleDataModel->searchRecords(array("NAME"=>$name, "EMAIL"=>$email));
				if(count($records) > 0){
					foreach($records as $record){
						if(strtolower($record->NAME) == $name){
							$this->action->addJSONItem("error", 1);
							$this->action->addJSONItem("message", $this->lang->line("error_username_exist"));
							continue;
						}

						if(strtolower($record->EMAIL) == $email){
							$this->action->addJSONItem("error", 1);
							$this->action->addJSONItem("message", $this->lang->line("error_email_exist"));
							continue;
						}
					}
				}else{

					$this->load->helper("string");
					
					// ok, seems we are ready to insert her into our database and celebrate her marriage
					// plus here we generate a random unique id for her
					$fieldsValues = array(
						"UNIQUE_ID"				=> random_string('unique'),
						"NAME"					=> $name,
						"EMAIL"					=> $email,
						"ENCRYPTED_PASSWORD"	=> md5($password),
						"CREATED_AT"			=> @date("Y-m-d H:i:s")
					);
					$result = $this->simpleDataModel->insert($fieldsValues);

					// see if she is successfuly inserted
					if($result){
						$this->action->addJSONItem("success", 1);
						$this->action->addJSONItem("error", 0);
						$this->action->addJSONItem("uid", $fieldsValues["UNIQUE_ID"]);
						$this->action->addJSONItem("user", array(
						"id"		=> $result,
						"name"		=> $fieldsValues["NAME"],
						"email"		=> $fieldsValues["EMAIL"],
						"created_at"=> $fieldsValues["CREATED_AT"],
						"updated_at"=> NULL
						));
					}else{
						$this->action->addJSONItem("error", 1);
						$this->action->addJSONItem("message", $this->lang->line("error_insert"));
					}
				}
			}else{
				$this->action->addJSONItem("error", 1);
				$this->action->addJSONItem("message", $this->lang->line("error_password"));
			}
		}else{
			$this->action->addJSONItem("error", 1);
			$this->action->addJSONItem("message", $this->lang->line("error_inputs"));
		}

		// informing UI about the result
		$this->action->flushJSON();
	}



	/**
	 *
	 * User login action
	 */
	public function login(){
		$this->action->userLogin(TRUE);
	}
}